home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
YERK
/
SUPPLEME
/
UNSUPPOR
/
OPTIONAL
/
LINKEDLI.ST
< prev
next >
Wrap
Text File
|
1986-02-06
|
7KB
|
283 lines
\ This file contains classes to support the data structures: queues and
\ linked lists.
\ 11/12/84 rw
\ 12/26/85 rfd Added stack notation
\ Incramented size by 1 in SetData
\ 12/27/85 rfd Print routine traverses links
\ Modified after:
\ 12/30/85 rfd Modified delete: & size:
\ 1/16/86 rfd Change EOLL from -1 to -FFFF
\ Mdified AFTER, BEFORE, PRINT, CREATE, SETDATA
:CLASS LinkArray <Super Object 8 <Indexed
\ ( n -- nextval )
:M NEXT: ^elem w@ ;M
\ ( n -- prevval )
:M PREV: ^elem 2+ w@ ;M
\ ( n -- data )
:M GETDATA: ^elem 4+ @ ;M
\ ( nextval n -- )
:M SETNEXT: ^elem w! ;M
\ ( prev index -- )
:M SETPREV: ^elem 2+ w! ;M
\ ( data index -- )
:M SETDATA: ^elem 4+ ! ;M
;CLASS
\ LinkedList - The usual gimmickry. Should have all the operations
\ anyone could ever want. Note though that it is implemented as a
\ circular linked list, for full generality. To treat it as a circular
\ linked list though, use the subclass CircleList.
hex -FFFF Constant EOLL decimal ( End Of LinkedList indicator )
22 Constant FRONTconst
39 Constant BACKconst
:CLASS LinkedList <Super Object
Int Front
Int Back
Int Current
Int Size
var poolsize
var thePool
var FreeListHead
\ ( -- data )
:M GetData:
get: size 0=
IF
EOLL
ELSE
get: current GetData: [ get: thePool ]
THEN
;M
\ ( data -- )
:M SetData:
get: size 0 =
IF 1 put: size
THEN
get: current SetData: [ get: thePool ]
depth 0 do drop loop
;M
\ ( idx -- )
:M AddFree:
get: FreeListHead -1 =
IF ( nothing in FreeList )
dup put: FreeListHead
-1 swap setNext: [ get: thePool ]
ELSE
dup get: FreeListHead swap SetNext: [ get: thePool ]
put: FreeListHead
THEN
;M
\ ( -- idx )
:M GetFree:
get: FreeListHead -1 =
IF ( nothing in FreeList )
( get more Pool )
classerr" 157
ELSE
get: FreeListHead next: [ get: thePool ] -1 =
IF ( one thing in FreeList )
get: FreeListHead
-1 Put: FreeListHead
1 +: size
ELSE ( Many Things in FreeList )
get: FreeListHead
dup next: [ get: thePool ]
put: FreeListHead
1 +: size
THEN
THEN
;M
\ ( -- current )
:M GetCurrent:
get: current
;M
\ ( current -- )
:M SetCurrent:
put: current
;M
\ ( -- data )
:M Next:
get: current get: back = get: size 0= or
IF
EOLL
ELSE
get: current next: [ get: thePool ] dup
put: current
getData: [ get: thePool ]
THEN
;M
\ ( -- data )
:M Prev:
get: current get: front = get: size 0= or
IF
EOLL
ELSE
get: current prev: [ get: thePool ] dup
put: current
getData: [ get: thePool ]
THEN
;M
\ ( -- data )
:M Front:
get: size 0=
IF
EOLL
ELSE
get: front dup put: Current
getData: [ get: thePool ]
THEN
;M
\ ( data -- )
:M Before:
get: poolsize get: size =
IF
." linked list full not added " drop
ELSE
GetFree: self
Get: current prev: [ get: thePool ] ( data new new prev -- )
2dup swap SetPrev: [ get: thePool ] drop
Get: current ( data new new current -- )
2dup SetPrev: [ get: thePool ]
2dup swap SetNext: [ get: thePool ]
get: front =
IF
dup put: front
ELSE
dup dup prev: [ get: thePool ]
SetNext: [ get: thePool ]
THEN
dup put: current
SetData: [ get: thePool ]
THEN
;M
\ ( data -- )
:M Create: { \ curr data new -- }
-> data
get: current -> curr
getFree: self -> new ( data new )
EOLL new setNext: [ get: thePool ] ( data new )
curr new setPrev: [ get: thePool ] ( data new )
new curr setnext: [ get: thepool ]
new put: back ( data new )
new put: current ( data new )
data setdata: self
;M
\ ( data -- )
:M After:
get: poolsize get: size =
IF
." linked list full not added " drop
ELSE
get: current
next: self prev: self drop EOLL = ( data new new next -- )
IF
put: current
Create: self
ELSE
drop getfree: self
getcurrent: self
2dup swap
setPrev: [ get: thePool ]
2dup next: [ get: thePool ]
2dup swap setnext: [ get: thePool ]
setprev: [ get: thepool ]
2dup setnext: [ get: thepool ] drop
dup put: current
setdata: [ get: thepool ]
THEN
THEN
;M
:M Delete:
get: size 0 = abort" LinkedList is Empty "
get: size 1 - put: size
get: current prev: [ get: thePool ] ( prev -- )
get: current next: [ get: thePool ] ( prev next -- )
next: self prev: self drop EOLL =
IF
ELSE
2dup SetPrev: [ get: thePool ]
THEN
swap setNext: [ get: thePool ]
FRONTconst
get: current get: back =
IF
drop BACKconst get: current prev: [ get: thePool ]
put: back
THEN
get: current get: front =
IF
get: current next: [ get: thePool ]
put: front
THEN
FRONTconst =
IF
get: current dup next: [ get: thePool ]
put: current
ELSE
get: current dup prev: [ get: thePool ]
put: current
THEN
( old current is on stack )
addFree: self
;M
:M Size: get: size ;M
\ ( maxindx -- )
:M Classinit: 1 put: size
put: poolsize
get: poolsize Heap> linkArray put: thePool
EOLL get: FreeListHead setNext: [ get: thePool ]
get: poolsize 1
DO
get: FreeListHead i setNext: [ get: thePool ]
i put: freeListHead
LOOP
0 setcurrent: self EOLL setdata: self 0 put: size
;M
:M Print:
size: self 0 =
IF ." list empty "
ELSE
front: self
getcurrent: self . . cr
BEGIN
next: self
dup EOLL =
IF drop 1
ELSE getcurrent: self . . cr 0
THEN
UNTIL
THEN
front: self drop
;M
;CLASS